package edu.uoc.tfc.web.tags;



import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;



public class DateTag extends AbstractFormFieldTag
{



    private static final long serialVersionUID = 1L;



	private static final Logger LOG = Logger.getLogger(DateTag.class);



    private boolean time = false;



	public boolean isTime() {
	    return time;
    }

	public void setTime(boolean time) {
	    this.time = time;
    }



	@Override
    protected void doStartFormField(JspWriter out) throws JspException
	{
		try
		{
			if (isWritable())
			{
	            if (isTime())
	            {
	            	String dateValue;
	            	String timeValue;
	            	if (StringUtils.isNotBlank(getValue()))
	            	{
	            		String[] values = getValue().split(" ");
	            		dateValue = values[0];
	            		timeValue = values[1];
	            	}
	            	else
	            	{
	            		dateValue = "";
	            		timeValue = "";
	            	}

	            	write(out, "<input class=\"" + getClassMode() + "\" type=\"text\" name=\"" + getName() + "-date\" id=\"" + getId() + "-date\" value=\"" + dateValue + "\" />");
	            	write(out, "<input class=\"" + getTimeClassMode() + "\" type=\"text\" name=\"" + getName() + "-time\" id=\"" + getId() + "-time\" value=\"" + timeValue + "\" />");
	            	write(out, "<li class=\"icon-calendar\"></li>");
	            	write(out, "<input type=\"hidden\" name=\"" + getName() + "\" id=\"" + getId() + "\"/>");
    				write(out, "<script type=\"text/javascript\">");
    				write(out, "  $(document).ready(function() {");
    				write(out, "    prepareDateField(\"" + getId() + "\");");
    				write(out, "  });");
    				write(out, "</script>");
	            }
	            else
	            {
	            	write(out, "<input class=\"" + getClassMode() + "\" type=\"text\" name=\"" + getName() + "\" id=\"" + getId() + "\" value=\"" + getValue() + "\" />");
	            	write(out, "<li class=\"icon-calendar\"></li>");
	            }
			}
			else
			{
				write(out, "<span class=\"" + getClassMode() + "\">" + getValue() + "</span>");
				write(out, "<li class=\"icon-calendar\"></li>");
				write(out, "<input type=\"hidden\" name=\"" + getName() + "\" id=\"" + getId() + "\" value=\"" + getValue() + "\" />");
			}
		}
		catch (Exception ex)
		{
			String message = "Error generando Campo de fecha/hora: " + this.getProperty();
			LOG.error(message, ex);
			throw new JspException(message, ex);
		}
	}

	private String getIdInitJs() {
		if (isTime()) {
			return getId() + "-date";
		} else {
			return getId();
		}
	}


	@Override
    protected void doEndFormField(JspWriter out) throws JspException
    {
		try
		{
			if (isWritable())
			{
				write(out, "<script type=\"text/javascript\">");
				write(out, "  $(document).ready(function() {");
				write(out, "    $('#" + getIdInitJs() + "').date_input();");
				write(out, "  });");
				write(out, "</script>");
				
//				write(out,"<script>");
//				write(out,"$($.date_input.initialize);");
//				write(out,"</script>");
			}
		}
		catch (Exception ex)
		{
			String message = "Error finalizado Campo de fecha/hora: " + ex.getMessage();
			LOG.error(message, ex);
			throw new JspException(message, ex);
		}
	}



	protected String getClassMode()
	{
		StringBuilder classMode = new StringBuilder("date_input");
		
		// TODO
		classMode.append(" span10");
		
		if (isTime() && isWritable())
		{
			classMode.append(" input-small");
		}
		else
		{
			classMode.append(" input-medium");
		}
		if (isWritable())
		{
			if (isRequired())
			{
				classMode.append(" emkt-required");
			}
			classMode.append(" focused");
		}
		else
		{
			classMode.append(" uneditable-input");
		}
		return classMode.toString();
	}



	protected String getTimeClassMode()
	{
		StringBuilder classMode = new StringBuilder("time_input input-small");
		if (isWritable())
		{
			if (isRequired())
			{
				classMode.append(" emkt-required");
			}
			classMode.append(" focused");
		}
		else
		{
			classMode.append(" uneditable-input");
		}
		return classMode.toString();
	}



}
